package com.mzkj.wanjulaile.mobile.api.modules.weChat.service;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.mzkj.common.utils.HttpUtils;
import com.mzkj.wanjulaile.mobile.api.modules.weChat.entity.AuthToken;
import com.mzkj.wanjulaile.mobile.api.modules.weChat.entity.UserInfoWX;
import com.mzkj.wanjulaile.mobile.api.modules.weChat.utils.WechatUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * mallService
 * Created by wangxw on 16/5/12.
 */
@Service("mallService")
public class MallService {

    /**
     * 日志对象
     */
    protected Logger logger = LoggerFactory.getLogger(getClass());

    public UserInfoWX getUserInfoWx(String openid, String accessToken) throws IOException {
        String json = HttpUtils.doGet(WechatUtils.USER_INFO_URL + "access_token=" + accessToken + "&openid=" + openid + "&lang=zh_CN");
        //解析返回结果
        ObjectMapper om = new ObjectMapper();
        return om.readValue(json, UserInfoWX.class);
    }

    public UserInfoWX getUserInfoWx(AuthToken authToken) throws IOException {
        String accessToken = authToken.getAccess_token();
        String openid = authToken.getOpenid();
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("access_token", accessToken);
        map.put("openid", openid);
        // TODO 接口参数顺序必须与API一致
        String json = HttpUtils.doGet(WechatUtils.USER_INFO_URL, map);
        //解析返回结果
        ObjectMapper om = new ObjectMapper();
        return om.readValue(json, UserInfoWX.class);
    }

}
